package com.dzj.fishingcommunity.service.impl;

import com.dzj.fishingcommunity.mapper.CoolFishSportMapper;
import com.dzj.fishingcommunity.model.CoolFishSport;
import com.dzj.fishingcommunity.service.CoolFishSportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CoolFishSportServiceImpl implements CoolFishSportService {

    @Autowired
    private CoolFishSportMapper coolFishSportMapper;

    @Override
    public Page<CoolFishSport> getAllCoolFishSports(Pageable pageable) {
        return coolFishSportMapper.findAll(pageable);
    }

    @Override
    public CoolFishSport getCoolFishSportById(String id) {
        return coolFishSportMapper.findById(id).orElse(null);
    }

    @Override
    public CoolFishSport addCoolFishSport(CoolFishSport coolFishSport) {
        coolFishSport.setCreatedAt(new Date());
        coolFishSport.setUpdatedAt(new Date());
        return coolFishSportMapper.save(coolFishSport);
    }

    @Override
    public CoolFishSport updateCoolFishSport(String id, CoolFishSport coolFishSport) {
        CoolFishSport existingCoolFishSport = getCoolFishSportById(id);
        if (existingCoolFishSport != null) {
            existingCoolFishSport.setAreaName(coolFishSport.getAreaName());
            existingCoolFishSport.setLatitude(coolFishSport.getLatitude());
            existingCoolFishSport.setLongitude(coolFishSport.getLongitude());
            existingCoolFishSport.setIsFishable(coolFishSport.getIsFishable());
            existingCoolFishSport.setReviewStatus(coolFishSport.getReviewStatus());
            existingCoolFishSport.setPopularity(coolFishSport.getPopularity());
            existingCoolFishSport.setUpdatedAt(new Date());
            return coolFishSportMapper.save(existingCoolFishSport);
        }
        return null;
    }

    @Override
    public void deleteCoolFishSport(String id) {
        coolFishSportMapper.deleteById(id);
    }
}